/*
M404SD01BA.cpp
Written by Jeff Eaton, 2014 - jeffeats@gmail.com

- MIT License -
Copyright (c) 2014, Jeff Eaton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

This software uses the SendOnlySoftwareSerial library adapted from SoftwareSerial by Nick Gammon 28th June 2012
*/

#include <Arduino.h>
#include "M404SD01BA.h"

// at 9600 baud on my device, there needs to be ~200+ microseconds between bytes or the data will get scrambled
// this is handled by reading the busy signal and only transmitting when it's low (idle)

M404SD01BA::M404SD01BA(int txPin, int busyPin, long baudRate) : _serialInterface(txPin) {
	_txPin = txPin;
	pinMode(_txPin, OUTPUT);
	digitalWrite(_txPin, HIGH);

	_busyPin = busyPin;
	pinMode(_busyPin, INPUT);

	_baudRate = baudRate;
	_serialInterface.begin(_baudRate);
}

M404SD01BA::~M404SD01BA() {
	// nothing to destruct
}

void M404SD01BA::waitForBusy() {
	while(digitalRead(_busyPin) == HIGH)
		delayMicroseconds(20);
}

void M404SD01BA::write(char s) {
	waitForBusy();
	_serialInterface.write(s);
}

void M404SD01BA::print(char s[]) { // requires a properly formatted string ending with a null (0x00) byte
	int i=0;
	do {
		waitForBusy();
		_serialInterface.write(s[i]);
		i++;
	}while(s[i] != 0x00);
}

void M404SD01BA::defineCustomChar(int pos, int data[5]) { // 5 data bytes
	if(pos > 2 || pos < 0)
		return;
	/*
	int b1 = data[0][0]*2^7 + data[0][1]*2^6 + data[0][2]*2^5 + data[0][3]*2^4 + data[0][4]*2^3 + data[1][0]*2^2 + data[1][1]*2^1 + data[1][2]*2^0;
	int b2 = data[1][3]*2^7 + data[1][4]*2^6 + data[2][0]*2^5 + data[2][1]*2^4 + data[2][2]*2^3 + data[2][3]*2^2 + data[2][4]*2^1 + data[3][0]*2^0;
	int b3 = data[3][1]*2^7 + data[3][2]*2^6 + data[3][3]*2^5 + data[3][4]*2^4 + data[4][0]*2^3 + data[4][1]*2^2 + data[4][2]*2^1 + data[4][3]*2^0;
	int b4 = data[4][4]*2^7 + data[5][0]*2^6 + data[5][1]*2^5 + data[5][2]*2^4 + data[5][3]*2^3 + data[5][4]*2^2 + data[6][0]*2^1 + data[6][1]*2^0;
	int b5 = data[6][2]*2^7 + data[6][3]*2^6 + data[6][4]*2^5;
	*/
	waitForBusy();
	_serialInterface.write(0x03);
	waitForBusy();
	_serialInterface.write(0xFC + pos);
	waitForBusy();
	_serialInterface.write(data[0]);
	waitForBusy();
	_serialInterface.write(data[1]);
	waitForBusy();
	_serialInterface.write(data[2]);
	waitForBusy();
	_serialInterface.write(data[3]);
	waitForBusy();
	_serialInterface.write(data[4]);
}

void M404SD01BA::dim(int level) {
	waitForBusy();
	_serialInterface.write(0x04);
	switch(level) {
	case 0: // 100%
		waitForBusy();
		_serialInterface.write(0xFF);
		break;
	case 1: // 60%
		waitForBusy();
		_serialInterface.write(0x60);
		break;
	case 2: // 40%
		waitForBusy();
		_serialInterface.write(0x40);
		break;
	case 3: // 20%
		waitForBusy();
		_serialInterface.write(0x20);
		break;
	default: // default to 100%
		waitForBusy();
		_serialInterface.write(0xFF);
		break;
	}
}

void M404SD01BA::backspace() {
	waitForBusy();
	_serialInterface.write(0x08);
}

void M404SD01BA::clear() {
	waitForBusy();
	_serialInterface.write(0x0B);
}

void M404SD01BA::crlf() {
	waitForBusy();
	_serialInterface.write(0x0D);
}

void M404SD01BA::position(int row, int col) { // rows 0-3, cols 0-39
	waitForBusy();
	_serialInterface.write(0x10);
	waitForBusy();
	_serialInterface.write(40*row + col);
}

void M404SD01BA::mode(int mode) { // 0 = normal, 1 = vertical scroll, 2 = overwrite
	waitForBusy();
	_serialInterface.write(0x11 + mode); // 0x11 = normal, 0x12 = vertical scroll, 0x13 = overwrite
}

void M404SD01BA::blank() {
	waitForBusy();
	_serialInterface.write(0x14);
}

void M404SD01BA::restore() {
	waitForBusy();
	_serialInterface.write(0x15);
}

void M404SD01BA::writePosUp() {
	waitForBusy();
	_serialInterface.write(0x1A);
}

void M404SD01BA::writePosDown() {
	waitForBusy();
	_serialInterface.write(0x1B);
}

void M404SD01BA::writePosRight() {
	waitForBusy();
	_serialInterface.write(0x1C);
}

void M404SD01BA::writePosLeft() {
	waitForBusy();
	_serialInterface.write(0x1D);
}

void M404SD01BA::reset() {
	waitForBusy();
	_serialInterface.write(0x1F);
}